package com.apes.scm.stock.profitAndLoss;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.account.model.InventoryBatch;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ProfitAndLossItem.CONST_TABLE_NAME)
public class ProfitAndLossItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "stock_profit_loss_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "损溢订单行")
    @Comment("损溢订单行")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "profitAndLoss_id", referencedColumnName = "id")
    @Comment("损溢订单号")
    private ProfitAndLoss profitAndLoss;

    @Column(name = "source_item_id", length = 30)
    @Comment("来源单明细单号")
    private String sourceItemId;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product; //商品

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "batch_code",referencedColumnName = "id")
    @Comment("商品批次内码")
    private InventoryBatch inventoryBatch; // 报损时选择批次

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位")
    private ProductUom uom; //计量单位

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("税率")
    private Tax tax; //税率

    @Column(name = "qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("损溢数量")
    private double qty = 0;

    @Column(name = "loss_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("可报损数")
    private double lossQty = 0;

    @Transient
    private double total = 0;

    public double getTotal() {
        return qty * priceUnit;
    }

    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("单价")
    private double priceUnit = 0; // 报溢时取价格文件（本公司）; 报损时有具体责任人时按填写单价，默认为0

    @Column (name ="note")
    @Comment("备注")
    private String note;

}
